package com.dormitory.management.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.management.system.entity.User;
import com.dormitory.management.system.enums.UserEnum;
import com.dormitory.management.system.mapper.HousemasterMapper;
import com.dormitory.management.system.mapper.StudentMapper;
import com.dormitory.management.system.req.UserPageReq;
import com.dormitory.management.system.req.UserUpdateReq;
import com.dormitory.management.system.resp.StuAndHouResp;
import com.dormitory.management.system.resp.UserResp;
import com.dormitory.management.system.api.UserInfoApi;
import com.dormitory.management.system.mapper.UserMapper;
import com.dormitory.management.system.service.UserService;
import com.dormitory.managemnet.util.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author world
 * @since 2021-010-9
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private HousemasterMapper housemasterMapper;

    @Override
    public boolean update(UserUpdateReq req) {
        return this.baseMapper.updateById(BeanUtil.copyProperties(req,User.class))>0;
    }

    @Override
    public IPage<UserResp> getPage(UserPageReq req) {
        Page<UserResp> page = new Page<>(req.getPageCurrent(),req.getPageSize());
        QueryWrapper<UserResp> wrapper = new QueryWrapper<>();
        wrapper.like(req.getUsername()!=null,"username",req.getUsername())
                .eq(req.getUserType()!=null,"role_key",req.getUserType())
                .ge(req.getGmtStart()!=null,"gmt_create",req.getGmtStart())
                .le(req.getGmtEnd()!=null,"gmt_create",req.getGmtStart())
                .eq("u.status_id",0)
                .eq("ur.status_id",0)
                .eq("r.status_id",0);
        return this.baseMapper.getUserPage(page, wrapper);
    }

    @Override
    public String getRoleKey(String username) {
        UserInfoApi user = this.baseMapper.queryUserInfo(new QueryWrapper<UserInfoApi>()
                .eq("username", username)
                .eq("user.status_id",0)
                .eq("user_role.status_id",0)
                .eq("role.status_id",0));
        return user.getRoleKey();
    }

    @Override
    public StuAndHouResp getUserInfo(String username) {
        if (userService.getRoleKey(username).equals(UserEnum.USER_HOUSEMASTER.getRoleKey())) {
            return housemasterMapper.getHousemasterInfo(new QueryWrapper<StuAndHouResp>().eq("h.identity_card", username));
        }else if (userService.getRoleKey(username).equals(UserEnum.USER_STUDENT.getRoleKey())) {
            return studentMapper.getStudentInfo(new QueryWrapper<StuAndHouResp>().eq("s.stu_id", username));
        }else {
            return null;
        }

    }
}
